package my.learnDB.relation.dao;

import my.learnDB.relation.domain.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.transaction.Transactional;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext.xml"})
public class BCascade {

    @Autowired
    UserDao userDao;  // 维护了外键, 提示先保存从表

    @Autowired
    UserGiveUpDao userGiveUpDao;  // 放弃维护，不管从表

    @Autowired
    UserCascadeDao userCascadeDao;  // 放弃维护，但是有级联操作


    @Test
    @Transactional
    @Rollback(value = false)
    public void addWithCascade(){
        UserCascade userCascade = new UserCascade("userCascade");
        AccountCa accountCascade1 = new AccountCa("accountCascade");

        // 双方设置关系
        accountCascade1.setUser(userCascade);
        userCascade.getAccounts().add(accountCascade1);

        // Hibernate: insert into user (sex, username, password) values (?, ?, ?)
        // Hibernate: insert into account (money, name, userId) values (?, ?, ?)
        // 仅仅保存主表
        userCascadeDao.save(userCascade);
    }


    // 级联删除 对象.delete




}
